package com.web.my12306.dao.impl;

import com.web.my12306.dao.ITrainDao;
import com.web.my12306.pojo.Train;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class TrainDaoImpl extends BaseDao implements ITrainDao
{
    public boolean add(Train train)
    {
        try
        {
            update("insert into train(trainno,departure_time,arrival_time,dstation,terminal,price,state) values(?,?,?,?," +
                    "?,?,?)", train.getTrainno(), train.getDeparture_Time(), train.getArrival_Time(), train.getDstation
                    (), train.getTerminal(), train.getPrice(), train.getState());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return true;
    }

    public boolean editByTid(int tid,int state)
    {
        update("update train set state=? where tid=?",state,tid);
        return true;
    }

    @Override
    public int queryByTrainno(String trainno, Timestamp day)
    {
        int tid = -1;
        query("Select tid from train where trainno=? AND departure_time>? AND departure_time<adddate(? ,1)", trainno,
                day, day);
        try
        {
            if (rs.next())
            {
                tid = rs.getInt("tid");
            }
        } catch (SQLException e)
        {
            e.printStackTrace();
        } finally
        {
            closeAll();
        }
        return tid;
    }

	@Override
	public Train queryByTid(int tid)
	{
		Train train=new Train();
		try {
			query("select * from train where tid=?",tid);
			while(rs.next()) {
				train.setTid(rs.getInt("tid"));
				train.setTrainno(rs.getString("trainno"));
				train.setDeparture_Time(rs.getTimestamp("departure_Time"));
				train.setArrival_Time(rs.getTimestamp("arrival_Time"));
				train.setDstation(rs.getString("dstation"));
				train.setTerminal(rs.getString("terminal"));
				train.setPrice(rs.getDouble("price"));
				train.setState(rs.getInt("state"));
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}finally {
			closeAll();
		}
		return train;
	}
	
	/**
     * 实现查询所有车次信息
     */
	@Override
	public List<Train> getTrainAll() {
		List<Train> trainlist=new ArrayList<Train>();
		String sql="select * from train where state=1";
		query(sql);
		try {
			while(rs.next()) {
				Train train=new Train();
				train.setTid(rs.getInt("tid"));
				train.setTrainno(rs.getString("trainno"));
				train.setDeparture_Time(rs.getTimestamp("departure_Time"));
				train.setArrival_Time(rs.getTimestamp("arrival_Time"));
				train.setDstation(rs.getString("dstation"));
				train.setTerminal(rs.getString("terminal"));
				train.setPrice(rs.getDouble("price"));
				train.setState(rs.getInt("state"));
				trainlist.add(train);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			closeAll();
		}
		
		return trainlist;
	}

	/**
	 * 实现按多条件查询所有列车信息
	 */
	@Override
	public List<Train> getTrainBySelect(String dstation, String terminal) {
		List<Train> traincheck=new ArrayList<Train>();
		String sql="select * from train where dstation=? and terminal=? ";
		query(sql,dstation,terminal);
		try {
			while(rs.next()) {
				Train train=new Train();
				train.setTid(rs.getInt("tid"));
				train.setTrainno(rs.getString("trainno"));
				train.setDeparture_Time(rs.getTimestamp("departure_Time"));
				train.setArrival_Time(rs.getTimestamp("arrival_Time"));
				train.setDstation(rs.getString("dstation"));
				train.setTerminal(rs.getString("terminal"));
				train.setPrice(rs.getDouble("price"));
				train.setState(rs.getInt("state"));
				traincheck.add(train);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			closeAll();
		}
		
		return traincheck;
	}

}
